<template>
	<view>
		<view class="page">
			<view class="phone-font">请输入您需要绑定的新手机号</view>
			<u-form :model="form" ref="uForm">
				<u-form-item class="from" label="+86" prop="mobile">
					<u-input v-model="form.mobile" @blur="login()" placeholder="请输入手机号" />
				</u-form-item>

				<u-form-item class="from" label="验证码" prop="code">
					<u-input v-model="form.code" placeholder="请输入验证码" />
					<view right v-if="!isSend" class="code" @click="sendSms()">获取验证码</view>
					<view right v-if="isSend" class="is-send">{{leftLife}}秒后重发</view>
				</u-form-item>
			</u-form>
			<view class="button-big">
				<view class="button" @click="submit">确认更换</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					mobile: '',
					code: ''
				},
				isSend: false,
				leftLife: 60,
				cutDown: "",
				rules: {
					mobile: [{
							required: true,
							message: '请输入手机号',
							trigger: ['change', 'blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// this.$u.test.mobile()就是返回true或者false的
								return this.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						}
					],
					code: [{
						required: true,
						message: '请输入验证码',
						trigger: ['change', 'blur'],
					}]
				}
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			login() {
				let reg = /^[1][3,4,5,7,8,9][0-9]{9}$/
				if (!this.form.mobile) { //判断如果手机号为空，提示用户输入手机号
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					})
					return
				} else if (!reg.test(this.form.mobile)) { //判断手机号格式时候正确
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					})
					return
				}
			},
			submit() {
				if (!this.form.mobile) {
					uni.showToast({
						icon: 'none',
						title: '请输入手机号'
					})
					return
				}
				if (!this.form.code) {
					uni.showToast({
						icon: 'none',
						title: '请输入验证码'
					})
					return
				}

				this.$request.api.checkSms({
					mobile: this.form.mobile,
					mailCode: this.form.code
				}).then(res => {
						console.log(res)
						if (res.data.status == 200) {
							this.$request.api.updatePhone({
								user_id: uni.getStorageSync('rawData').userId,
								user_tel: this.form.mobile,

							}).then(res => {
								// this.code = res.code
								console.log(res)

								if (res.data.code == 200) {
									uni.showToast({
										icon: 'success',
										title: '修改成功',
										icon: 'none'
									})
									setTimeout(() => {
										console.log('{{{{{{{{{00000}}}}}}}}}')
										uni.navigateBack({
											delta: 1
										})
									}, 400)
									let rawData = uni.getStorageSync('rawData')
									rawData.userTel = this.form.mobile
									console.log(rawData.userTel)
									console.log(rawData)
									// rawData.userTel = this.form.mobile
									uni.setStorageSync('rawData', rawData)

								} else {
									uni.hideLoading();
									uni.showToast({
										title: res.data.msg,
										duration: 2000,
										icon: 'none'
									});
									return;
								}
							})

						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						}
					},
					err => {
						uni.hideLoading();
						uni.showToast({
							title: err.errorMessage,
							duration: 2000,
							icon: 'none'
						});
						return;
					});


			},
			sendSms() {
				if (!this.form.mobile) {
					uni.showToast({
						icon: 'none',
						title: '请输入手机号'
					})
					return
				}
				this.$request.api.sendSms({
					mobile: this.form.mobile
				}).then(res => {
						this.isSend = true;
						this.clearCut();
						this.leftLife = 60;
						this.cut();
						// this.code = res.code
						uni.hideLoading();
					},
					err => {
						uni.hideLoading();
						uni.showToast({
							title: err.errorMessage,
							duration: 2000,
							icon: 'none'
						});
						return;
					}
				)
			},

			cut() {
				this.cutDown = setInterval(e => {
					if (this.leftLife > 0) {
						this.leftLife--;
					} else {
						this.clearCut();
						this.isSend = false;
					}
				}, 1000);
			},
			clearCut() {
				clearInterval(this.cutDown);
			},
		}
	}
</script>

<style lang="scss">
	.page {
		margin-top: 11rpx;
		background-color: #FFFFFF;
		width: 100vw;
		height: 100vh;

		// padding-left: 70rpx;
		.phone-font {
			font-size: 36rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: #333333;
			padding: 70rpx 0rpx 0rpx 70rpx;
		}

		.from {
			margin: 65rpx;
			font-size: 22rpx;
			color: #999999;
			border-bottom: 2rpx solid #999999;

			.code {
				font-size: 34rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #1677FF;
			}

			.is-send {
				font-size: 15px;
				color: rgba(160, 160, 160, 1);
			}
		}

		.button-big {
			display: flex;
			text-align: center;
			justify-content: center;

			.button {
				width: 702rpx;
				height: 98rpx;
				// background: #578EF0;
				background: linear-gradient(rgb(118, 217, 4) 0%, rgb(65, 117, 5) 100%);
				box-shadow: 0rpx 4rpx 10rpx 0rpx rgba(87, 142, 240, 0.4);
				border-radius: 49rpx;
				color: #FFFFFF;
				font-weight: 500;
				font-size: 36rpx;
				line-height: 98rpx;
			}
		}
	}
</style>